package com.ggh.jinjilive.ui.mall.shop;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityAttentionShopBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.collection.CollectionModule;
import com.ggh.jinjilive.mobile.mall.shop.AttentionShopMobile;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mall.shop.adapter.AttentionShopAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 关注店铺
 */
@Route(path = ARouterUtils.AttentionShopActivity)
public class AttentionShopActivity extends BaseActivity<ActivityAttentionShopBinding> implements OnRefreshListener, OnLoadMoreListener,View.OnClickListener{
    //VM
    private ShopViewModel shopViewModel;

    //适配器
    private AttentionShopAdapter attentionShopAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        shopViewModel = new ViewModelProvider(this,viewModelFactory).get(ShopViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_attention_shop;
    }


    @Override
    public void initView() {
        super.initView();
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取订单信息
                shopViewModel.isRefresh = true;
                shopViewModel.collectStoreList();
            }
        });
        loadService.showCallback(LoadingCallback.class);
        //获取订单信息
        shopViewModel.isRefresh = true;
        shopViewModel.collectStoreList();

        setWhiteActionBarStyle("关注店铺",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
    }

    @Override
    public void initData() {
        super.initData();
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        //适配器
        attentionShopAdapter = new AttentionShopAdapter();
        attentionShopAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                if(position == 999999){
                    List<AttentionShopMobile> attentionShopMobileList = (List<AttentionShopMobile>) mObject;
                    boolean allSelected = true;
                    for (AttentionShopMobile attentionShopMobile : attentionShopMobileList){
                        if(!attentionShopMobile.isSelected()){
                            allSelected = false;
                            break;
                        }
                    }
                    binding.cbCheckall.setChecked(allSelected);
                }
            }
        });
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        binding.recyclerView.setAdapter(attentionShopAdapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //管理
        binding.managementButton.setOnClickListener(this);
        //全选
        binding.cbCheckall.setOnClickListener(this);
        //删除
        binding.ButtonImmediatelyBuy.setOnClickListener(this);

        //关注店铺的列表
        shopViewModel.mAttentionShopMobileList.observe(this, new Observer<List<AttentionShopMobile>>() {
            @Override
            public void onChanged(List<AttentionShopMobile> attentionShopMobiles) {
                binding.refreshLayout.finishRefresh();
                if(shopViewModel.isRefresh){
                    if(attentionShopMobiles.size() > 0){
                        loadService.showSuccess();
                        attentionShopAdapter.setAdd(shopViewModel.mAttentionShopMobileList.getValue());
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(attentionShopMobiles.size() > 0){
                        attentionShopAdapter.ADD(shopViewModel.mAttentionShopMobileList.getValue());
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
        //店铺是否关注
        shopViewModel.attention.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                //获取订单信息
                shopViewModel.isRefresh = true;
                shopViewModel.collectStoreList();
            }
        });
    }



    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取订单信息
        shopViewModel.isRefresh = true;
        shopViewModel.collectStoreList();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取订单信息
        shopViewModel.isRefresh = false;
        shopViewModel.collectStoreList();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.management_Button://管理
                if(binding.llDelete.getVisibility() == View.VISIBLE){
                    binding.llDelete.setVisibility(View.GONE);
                    binding.managementButton.setText("完成");
                    attentionShopAdapter.setManagement("完成");
                }else {
                    binding.llDelete.setVisibility(View.VISIBLE);
                    binding.managementButton.setText("管理");
                    attentionShopAdapter.setManagement("管理");
                }
                break;
            case R.id.Button_immediately_buy://删除
                if(!TextUtils.isEmpty(delete())){
                    //删除店铺
                    shopViewModel.delCollectStore(delete());
                }else {
                    ToastUtils.make().show("请先选择店铺");
                }
                break;
            case  R.id.cb_checkall://全选
                if(binding.cbCheckall.isChecked()){
                    selectAll(true);
                }else {
                    selectAll(false);
                }
                break;

        }
    }

    /**
     * 删除
     * @return
     */
    public String delete(){
        String deleteList = "";
        for (AttentionShopMobile attentionShopMobile : shopViewModel.mAttentionShopMobileList.getValue()){
            if(attentionShopMobile.isSelected()){
                if(TextUtils.isEmpty(deleteList)){
                    deleteList = attentionShopMobile.getId() +"";
                }else {
                    deleteList = deleteList + "," + attentionShopMobile.getId() +"";
                }
            }
        }
        return deleteList;
    }

    /**
     * 全选
     */
    private void selectAll(boolean selectAll){
        if(shopViewModel.mAttentionShopMobileList.getValue().size() > 0){
            for (int i = 0;i < shopViewModel.mAttentionShopMobileList.getValue().size() ;i++){
                shopViewModel.mAttentionShopMobileList.getValue().get(i).setSelected(selectAll);
            }
        }
        attentionShopAdapter.notifyDataSetChanged();
    }


}
